package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class StatementServiceImpl extends ServiceImpl<StatementMapper,Statement> implements IStatementService {

    @Autowired
    private cn.wolfcode.car.business.mapper.StatementMapper statementMapper;

    @Autowired
    private IStatementService statementService;

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private IStatementItemService statementItemService;

    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        IPage<Statement> page = new Page<>(qo.getPageNum(),qo.getPageSize());
        QueryWrapper<Statement> wrapper = Wrappers.<Statement>query();
        wrapper.like(StringUtils.hasText(qo.getCustomerName()),"customer_name",qo.getCustomerName());
        wrapper.like(StringUtils.hasText(qo.getCustomerPhone()),"customer_phone",qo.getCustomerPhone());
        wrapper.like(StringUtils.hasText(qo.getLicensePlate()),"license_plate",qo.getLicensePlate());

        IPage<Statement> statementIPage = statementMapper.selectPage(page, wrapper);
        return new TablePageInfo<Statement>(super.page(statementIPage,wrapper));
    }


    @Override
    public void insert(Statement statement) {
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        //数据合理化判断
        //只有在消费状态才能去编辑
        Statement oldObj = this.get(statement.getId());
        if (Statement.STATUS_PAID.equals(oldObj.getStatus())) {
            throw new BusinessException("只有在消费时才能进行编辑");
        }
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    @Transactional
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            //数据合理化判断
            //数据合理化判断
            //只有在消费状态才能去编辑
            Statement statement = statementService.get(dictId);
            if (Statement.STATUS_PAID.equals(statement.getStatus())) {
                throw new BusinessException("只有在消费时才能进行删除");
            }
            statementItemService.delete(dictId);
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public Statement selectByAppointmentId(Long appointmentId) {
        return statementMapper.selectByAppointmentId(appointmentId);
    }

    @Override
    public void payStatement(Long statementId) {
        //合理性判断
        //处于消费中才能进行支付
        Statement statement = statementService.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new BusinessException("只有处于消费中才能进行支付");
        }
        statementMapper.changeStatus(statementId,Statement.STATUS_PAID, ShiroUtils.getUserId());
        //如果结算单是预约单生成的
        if (statement.getAppointmentId()!=null){
            //需要根据预约单ID更新状态,更新成已结算
            appointmentService.changeStatus(statement.getAppointmentId(), Appointment.STATUS_SETTLE);
        }
    }
}
